package com.liang.codegenerate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liang.codegenerate.common.ErrorCode;
import com.liang.codegenerate.exception.BusinessException;
import com.liang.codegenerate.model.dto.fields.FieldsAddDto;
import com.liang.codegenerate.model.dto.fields.FieldsUpdateDto;
import com.liang.codegenerate.model.entity.Fields;
import com.liang.codegenerate.model.entity.Tables;
import com.liang.codegenerate.service.FieldsService;
import com.liang.codegenerate.mapper.FieldsMapper;
import com.liang.codegenerate.service.TablesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author lzw
* @description 针对表【fields(字段表)】的数据库操作Service实现
* @createDate 2024-01-20 21:08:33
*/
@Service
public class FieldsServiceImpl extends ServiceImpl<FieldsMapper, Fields>
    implements FieldsService{

    @Resource
    private TablesService tablesService;

    @Resource
    private FieldsMapper fieldsMapper;

    @Override
    public boolean addField(FieldsAddDto fieldsAddDto) {
        String field = fieldsAddDto.getField();
        String fieldsType = fieldsAddDto.getFieldsType();
        if (StringUtils.isAnyBlank(field,fieldsType)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long tablesId = fieldsAddDto.getTablesId();
        if (tablesId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Tables tables = tablesService.listTableById(tablesId);
        if (tables == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"数据表id无效");
        }
        Fields fields = new Fields();
        BeanUtils.copyProperties(fieldsAddDto,fields);
        boolean save = this.save(fields);
        if (!save){
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return true;
    }

    @Override
    public List<Fields> listField(Long tableId) {
        QueryWrapper<Fields> fieldsQueryWrapper = new QueryWrapper<>();
        fieldsQueryWrapper.eq("tables_id",tableId);
        return this.list(fieldsQueryWrapper);
    }

    @Override
    public Fields listFieldById(Long id) {
        QueryWrapper<Fields> fieldsQueryWrapper = new QueryWrapper<>();
        fieldsQueryWrapper.eq("id",id);
        return this.getOne(fieldsQueryWrapper);
    }

    @Override
    public boolean updateField(FieldsUpdateDto fieldsUpdateDto) {
        String field = fieldsUpdateDto.getField();
        String fieldsType = fieldsUpdateDto.getFieldsType();
        if (StringUtils.isAnyBlank(field,fieldsType)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<Fields> fieldsQueryWrapper = new QueryWrapper<>();
        fieldsQueryWrapper.eq("id",fieldsUpdateDto.getId());
        Fields fields = fieldsMapper.selectOne(fieldsQueryWrapper);
        if (fields == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        BeanUtils.copyProperties(fieldsUpdateDto,fields);
        boolean update = this.updateById(fields);
        if (!update){
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return true;
    }

    @Override
    public boolean deleteField(Long id) {
        QueryWrapper<Fields> fieldsQueryWrapper = new QueryWrapper<>();
        fieldsQueryWrapper.eq("id",id);
        boolean delete = this.remove(fieldsQueryWrapper);
        if (!delete){
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return true;
    }
}




